package com.xxkj.drugtraceability.ui.doctoradviceback

import android.text.Html
import android.view.LayoutInflater
import android.view.View
import androidx.core.content.ContextCompat
import androidx.core.widget.doAfterTextChanged
import androidx.databinding.DataBindingUtil
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.xxkj.drugtraceability.R
import com.xxkj.drugtraceability.bean.DoctorAdviceInfoBean
import com.xxkj.drugtraceability.databinding.LayoutDoctorBackViewBinding
import com.xxkj.drugtraceability.databinding.LayoutDoctoradvicecollectionBinding
import com.xxkj.drugtraceability.databinding.LayoutDrugScan2Binding
import com.xxkj.drugtraceability.ext.clickN

/**
 * author: zhoujingjin
 * created on: 2024/9/10 16:52
 * description:layout_doctoradvicecollection
 */
class DoctorAdviceBackAdapter(
    private val actionClick: (index: Int) -> Unit, private val action: () -> Unit
) : BaseQuickAdapter<DoctorAdviceInfoBean, BaseDataBindingHolder<LayoutDoctorBackViewBinding>>(R.layout.layout_doctor_back_view) {
    override fun convert(
        holder: BaseDataBindingHolder<LayoutDoctorBackViewBinding>, item: DoctorAdviceInfoBean
    ) {
        holder.dataBinding?.apply {

            doctorBackName.text = item.psnName
            doctorBackDate.text = "用药日期：${item.prescriptionTime?.substring(0, 10)}"
            doctorAdviceDetail.removeAllViews()
            doctorBack.clickN {
                actionClick.invoke(holder.adapterPosition)
            }
            if (!item.drugInfoList.isNullOrEmpty()) {
                item.drugInfoList.forEach { itemTemp ->
                    val viewDrug = LayoutInflater.from(context)
                        .inflate(R.layout.layout_doctoradvicecollection, null)
                    val bind = DataBindingUtil.bind<LayoutDoctoradvicecollectionBinding>(viewDrug)
                    bind?.apply {
                        val buffer = StringBuffer()
                        buffer.append("<font color='#33333F' size='50px'>")
                        buffer.append("<b>" + itemTemp.drugName + "    " + "</b>")
                        buffer.append("</font>")
                        buffer.append("<font color='#494953' size='24px'>")
                        buffer.append(itemTemp.drugSpec)
                        buffer.append("</font>")
                        if (itemTemp.trdnFlag == "1") {
                            //当前药品是否是拆零标识  1是 0否
                            try {
                                buffer.append("<img src='" + R.mipmap.icon_unpack + "'/>")
                                tvContent.text = Html.fromHtml(buffer.toString(), {
                                    var drawable =
                                        ContextCompat.getDrawable(context, R.mipmap.icon_unpack)
                                    drawable?.setBounds(
                                        0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight
                                    )
                                    drawable!!
                                }, null)
                            } catch (e: Exception) {
                                tvContent.text = Html.fromHtml(buffer.toString())
                            }
                        } else {
                            tvContent.text = Html.fromHtml(buffer.toString())
                        }
                        tvRight.text = itemTemp.drugNum?.toDouble()?.toInt()
                            ?.toString()/*+item.drugRetailSoec*/
                        if ("Y" == itemTemp.takeDrug) {
                            tvType.text = "出"
                            tvType.setBackgroundResource(R.drawable.item_out)
                        } else {
                            if ("1" == itemTemp.presType) {
                                tvType.text = "长"
                                tvType.setBackgroundResource(R.drawable.item_long)
                            } else if ("2" == item.presType) {
                                tvType.text = "临"
                                tvType.setBackgroundResource(R.drawable.item_short)
                            }
                        }
                        //溯源码
                        llAdd.removeAllViews()
                        if (itemTemp.drugTracInfoList == null) {
                            itemTemp.drugTracInfoList = mutableListOf()
                        }
                        if (itemTemp.drugTracInfoList.isNullOrEmpty()) {
                            llBottom.visibility = View.GONE
                        } else {
                            llBottom.visibility = View.VISIBLE
                            itemTemp.drugTracInfoList?.forEach { drugTrace ->
                                val viewDrug1 = LayoutInflater.from(context)
                                    .inflate(R.layout.layout_drug_scan_2, null)
                                val bind = DataBindingUtil.bind<LayoutDrugScan2Binding>(viewDrug1)
                                bind?.apply {
                                    data = drugTrace.drugTracCodg
                                    llMore.visibility = View.GONE
                                    ivDelete.visibility = View.VISIBLE
                                    ivDelete.tag = viewDrug1
                                    ivDelete.clickN {
                                        //删除
                                        itemTemp.drugTracInfoList?.remove(drugTrace)
                                        llAdd.removeView(ivDelete.tag as View)
                                        action.invoke()
                                    }
                                    if (itemTemp.trdnFlag == "1") {
                                        etDrugNum.visibility = View.VISIBLE
                                        if (drugTrace.drugTracCodeNum != null) {
                                            etDrugNum.setText(drugTrace.drugTracCodeNum.toString())
                                        }
                                        etDrugNum.doAfterTextChanged {
                                            try {
                                                drugTrace.drugTracCodeNum = Integer(it!!.toString())
                                            } catch (e: Exception) {
                                            }
                                        }
                                        etDrugNum.setOnFocusChangeListener { _, hasFocus ->
                                            if (!hasFocus && etDrugNum.text.isNullOrEmpty()) {
                                                drugTrace.drugTracCodeNum = Integer(1)
                                                etDrugNum.setText("1")
                                            }
                                        }
                                        etDrugNum.isEnabled = false
                                    } else {
                                        drugTrace.drugTracCodeNum = Integer(1)
                                    }
                                }

                                llAdd.addView(viewDrug1)
                            }
                        }

                    }
                    doctorAdviceDetail.addView(viewDrug)

                }
            }


        }

    }
}